Return to Stevenator's ASP Page

ASP Application: User Tracking

Click here to see who is currently viewing this site.

This code used to use the Scripting.Dictionary Object, which was originally listed in the registry as thread-safe. Using old versions of IIS, this code will operate, but under IIS 4.0 with the most recent updates, it would fail because the Dictionary object has been (correctly) listed as non-threadsafe in the registry. This causes an error in the global.asa when the the dictionary object is declared as an Application() variable. Currently, the application uses a SQL database to track users, which solves this problem.

It is very easy to track how many users are currently viewing your web site, including what page they are on, what IP address they are coming from, what browser they are using, and other useful information. In order to track the page they have accessed, you must include this piece of code at the top of every page that you wish to track:

	'Update Current Users Page
	set hdrconntemp=server.createobject("adodb.connection")
	hdrconntemp.open Session("aspdb_ConnectionString")
	hdrsqlString = "UPDATE UserTable SET currentpage = '" & Request.ServerVariables("SCRIPT_NAME") & "' WHERE session_id = " & Session.SessionID
	Set hdrlogRS=hdrconntemp.execute(hdrsqlstring)
	Set hdrlogRS=nothing
	Set hdrconntemp = Nothing

In order to track the other variables, you need to make this database call in your global.asa:

In Application_OnStart:
	Set Conn = Server.CreateObject("ADODB.Connection")
	'Set dsn, dbuser, dbpass to your variables
	sql = "DELETE FROM UserTable"
	Conn.Execute sql
	Set Conn = Nothing

In Session_OnStart:
	' Track which IP's are currently connected
	conntemp.Open Session("aspdb_ConnectionString")
	sqlString = "INSERT INTO UserTable (session_id, ip, browser, created) " & _
		" VALUES (" & Session.SessionID & ",'" & Request.ServerVariables("REMOTE_HOST") & "','" & Request.ServerVariables("HTTP_USER_AGENT") & "','" & now & "')"
	logRS.Open sqlString, conntemp
	Set logRS = Nothing
	Set conntemp = Nothing

In Session_OnEnd:
	'Remove user from UserTable
	set conntemp=server.createobject("adodb.connection")
	set logRS = Server.CreateObject("ADODB.RecordSet")
	conntemp.Open Session("aspdb_ConnectionString")
	sqlString = "DELETE FROM UserTable WHERE session_id = " & Session.SessionID
	logRS.Open sqlString, conntemp	
	Set logRS = Nothing
	Set conntemp = Nothing

Finally, in order to display the information, you will need to create your tracking page, which I have called whoson.asp, because it is loosely based on an application of that name found in Active Server Pages Unleashed. Here is the source code for whoson.asp(note that I call my database table currentuser, and that I use a function called DBQuery for my database call):

1    <% OPTION EXPLICIT %>
2    <!-- #INCLUDE VIRTUAL="stevesmith/top.asp" -->
3    <html>
4    <head>
5    <%
6    Dim rs
7    Dim MyServer
8    Dim MyPath
9    Dim MySelf
10   Dim objDNS
11   Dim cnt
12   
13   MyServer=Request.ServerVariables("SERVER_NAME")
14   MyPath=Request.ServerVariables("SCRIPT_NAME")
15   MySelf="HTTP://" & MyServer & MyPath
16   %>

17   <title>Who's On Stevenator's ASP Site</title>
18   <meta HTTP-EQUIV="REFRESH" CONTENT="20;<%=Myself%>">
19   <meta NAME="Author" CONTENT="Steven A. Smith">
20   <%
21    Set RS = Server.CreateObject("ADODB.RecordSet")
22    set RS = DBQuery("SELECT COUNT(*) AS cnt FROM currentuser")
23    cnt = RS("cnt")
24   
25    Set objDNS = Server.CreateObject("TCPIP.DNS")
26   %>

27   <STYLE TYPE="text/css">
28   P { text-indent:1cm; }
29   TD { font-size: 10pt;
30    font-style : normal;
31    font-family: sans-serif;}
32   </STYLE>
33   </head>
34   <body Background="images/whtmarb.jpg">
35   
36   <a HREF="Default.asp">
37   <img SRC="images/return.gif" ALIGN="LEFT" ALT="Return to Stevenator's ASP Page" WIDTH="40" HEIGHT="40"></a>
38   <center>
39   <b>Total Sessions: </b> <%=cnt%><br>
40   <b>Server Time: </b> <%=now%><br>
41   <b>Your IP: <font COLOR="RED"><%=Session("IP")%></font></b><br>
42   <B>Your Domain: <FONT COLOR="RED"><%=objDNS.GetHostByIP(Session("IP"))%></font></B><BR>
43   <table BORDER="1" BGCOLOR="#F3EFD0">
44   <tr BGCOLOR="#DFCBA0">
45   <th>Session</th>
46   <th>Current Page</th>
47   <th>Requesting IP</th>
48   <th>Browser</th>
49   <th>On Since</th>
50   </tr>
51   <%
52   
53    set RS = DBQuery("SELECT * FROM currentuser ORDER BY 2,1")
54    While Not RS.EOF
55   %>

56   <tr>
57   <td><%=RS(0)%></td>
58   <td><%=RS(2)%></td>
59   <td><%=RS(1)%></td>
60   <td><%=RS(4)%></td>
61   <td><%=RS(3)%></td>
62   </tr>
63   <%
64    RS.MoveNext
65    Wend
66    Set RS = Nothing
67   %>

68   </table>
69   </center>
70   </body>
71   </html>

Main | FAQ | Tutorials | Samples | Other | Privacy
Steven Smith, MCSE + Internet, MCSD
Last Modified: 3/23/2000 7:27:33 AM
History: 3/23/2000 7:27:33 AM
Valid HTML 4.0!